package com.yanqu.road.server.manager.union;

import com.yanqu.road.entity.enums.eUnionDeleteState;
import com.yanqu.road.entity.log.*;
import com.yanqu.road.logic.bussiness.LogDaoHelper;
import com.yanqu.road.logic.bussiness.log.LogBussiness;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class CrossUnionLogMgr {

    private static List<LogUnionUpgradeCross> logUnionUpgradeCrossList = new ArrayList<>();
    private static List<LogCrossUserReward> logCrossUserRewardList = new ArrayList();


    public static void addLogUnionUpgradeCross(int groupId, String unionUid, long serverId, long masterUserId){
        LogUnionUpgradeCross log = new LogUnionUpgradeCross(groupId, unionUid, serverId, masterUserId);
        synchronized (logUnionUpgradeCrossList){
            logUnionUpgradeCrossList.add(log);
        }
    }

    private static void saveLogUnionUpgradeCross(){
        if (!CollectionUtils.isEmpty(logUnionUpgradeCrossList)) {
            List<LogUnionUpgradeCross> list;
            synchronized (logUnionUpgradeCrossList) {
                list = new ArrayList<>(logUnionUpgradeCrossList);
                logUnionUpgradeCrossList.clear();
            }
            LogBussiness.addLogUnionUpgradeCross(list);
        }
    }

    public static void addLogCrossUserReward(long userId, long serverId, int systemId, int rewardType, String reward, String param){
        LogCrossUserReward log = new LogCrossUserReward(userId, serverId, systemId, rewardType, reward, param);
        synchronized (logCrossUserRewardList){
            logCrossUserRewardList.add(log);
        }
    }

    private static void saveLogCrossUserReward(){
        if (!CollectionUtils.isEmpty(logCrossUserRewardList)) {
            List<LogCrossUserReward> list;
            synchronized (logCrossUserRewardList) {
                list = new ArrayList<>(logCrossUserRewardList);
                logCrossUserRewardList.clear();
            }
            LogBussiness.addLogCrossUserReward(list);
        }
    }

    public static void save(){
        saveLogUnionUpgradeCross();
        saveLogCrossUserReward();
    }
}
